<template>
    <el-row :gutter="20" id="backcard" v-cloak>
        <el-col :span="11">
            <div class="block mt">退款信息</div>
            <el-tag type="info" class="block mt">退款客户：{{back.name}}</el-tag>
            <el-tag type="info" class="block mt">退款金额：{{back.money}}</el-tag>
            <el-tag type="info" class="block mt">退款描述：{{back.remarks}}</el-tag>
            <el-tag type="info" class="block mt">退款时间：{{times(back.addtime)}}</el-tag>
            <el-tag type="info" class="block mt">退款类型：{{back.isstore?'退卡转余额':'仅退款'}}</el-tag>
            <el-table :data="backinfo" :stripe="true" height="400" v-show="backinfo.length">
                <el-table-column type="index" label="#"></el-table-column>
                <el-table-column prop="name" label="项目"></el-table-column>
                <el-table-column prop="cardtype" label="所属类型">
                    <template slot-scope="scope">
                        <el-tag size="mini" type="primary" v-if="scope.row.cardtype==1">期卡</el-tag>
                        <el-tag size="mini" type="success" v-else>疗程卡</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="当前状态">
                    <template slot-scope="scope">
                        <el-tag type="info" size="mini">已弃用</el-tag>
                    </template>
                </el-table-column>
            </el-table>
        </el-col>
        <el-col :span="13">
            <el-button size="mini" type="primary" icon="el-icon-plus" class="fr mt" @click="addaccount">退款账户</el-button>
            <el-table :data="accountlist" :stripe="true" class="mt" height="200">
                <el-table-column prop="name" label="账户名称"></el-table-column>
                <el-table-column prop="accounttype" label="账户类型">
                    <template slot-scope="scope">
                        <el-tag size="mini" type="success" v-if="scope.row.accounttype===1">现金</el-tag>
                        <el-tag size="mini" type="primary" v-if="scope.row.accounttype===2">银行卡</el-tag>
                        <el-tag size="mini" type="warning" v-if="scope.row.accounttype===3">移动支付</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="paymoney" label="出款金额">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.paymoney" size="mini" @change="chanpaymoney"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="移除">
                    <template slot-scope="scope">
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeAcco(scope.$index)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-tag size="mini" type="danger" class="fr mb">应退金额：{{accoumoney}} 元</el-tag>
            <div class="block mt mb ov clear">
                <el-alert type="info" title="如遇员工离职等，则添加店长" class="w300 fl"></el-alert>
                <el-button size="mini" type="primary" icon="el-icon-plus" @click="addusers" class="fr">业绩人员</el-button>
            </div>
            <el-table :data="userlist" :stripe="true" height="200">
                <el-table-column prop="name" label="员工姓名"></el-table-column>
                <el-table-column prop="username" label="员工电话"></el-table-column>
                <el-table-column prop="feat" label="业绩扣除" width="100">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.feat" size="mini" @change="chanfeat"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="移除">
                    <template slot-scope="scope">
                        <el-button type="danger" icon="el-icon-delete" @click="removeUser(scope.$index)" size="mini"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-tag size="mini" type="danger" class="fr mb">应扣业绩：{{allotmoney}} 元</el-tag>
            <div class="block right clear mt mb">
                <el-button type="success" icon="el-icon-upload" @click="submit">确定审核</el-button>
                <el-button type="danger" icon="el-icon-close" @click="closewin">取消关闭</el-button>
            </div>
        </el-col>
    </el-row>
</template>
<script>
import C from '@/common/common.js'
import P from '@/service/shop/popup.js'

export default {
  name: 'checkBackCard',
  data () {
    return {
      back: {}, // 退卡的基本信息
      backinfo: [], // 退卡的卡项列表
      accountlist: [], // 出款账户列表
      userlist: [], // 业绩分配人员
      money: 0,
      request: C.getDatas()
    }
  },
  methods: {

    addaccount: async function () {
      let res = await P.accountPopup({})
      if (res) {
        res.paymoney = 0
        this.accountlist.push(res)
      }
    },

    addusers: async function () {
      let res = await P.userPopup({ gid: this.request.gid })
      if (res) {
        res.feat = 0
        this.userlist.push(res)
      }
    },

    chanpaymoney: function () {
      this.accountlist = JSON.parse(JSON.stringify(this.accountlist))
      this.accountlist.forEach(function (e) {
        if (e.balance - e.paymoney < 0) {
          e.paymoney = 0
          C.error('账户 “' + e.name + '” 余额不足，请更换账户！')
          return false
        }
      })
    },

    chanfeat: function () {
      this.userlist = JSON.parse(JSON.stringify(this.userlist))
    },

    removeAcco: function (index) {
      this.accountlist.splice(index, 1)
    },

    removeUser: function (index) {
      this.userlist.splice(index, 1)
    },

    submit: async function () {
      if (this.accoumoney != 0) {
        C.error('应出款金额不正确')
        return false
      } else if (this.allotmoney != 0) {
        C.error('应扣业绩不正确')
        return false
      }

      let alist = [],
        ulist = []

      for (var i = this.accountlist.length - 1; i >= 0; i--) {
        if (this.accountlist[i].paymoney > 0) {
          alist.push({
            id: this.accountlist[i].id,
            paymoney: this.accountlist[i].paymoney
          })
        }
      }

      for (var k = this.userlist.length - 1; k >= 0; k--) {
        if (this.userlist[k].feat > 0) {
          ulist.push({
            id: this.userlist[k].id,
            feat: this.userlist[k].feat,
            bumenid: this.userlist[k].bumenid,
            xiaozuid: this.userlist[k].xiaozuid
          })
        }
      }

      let data = {
        bid: this.back.id,
        alist: alist,
        ulist: ulist
      }

      let res = await C.ajax('/shop/backcard/checkbackcard', { data: JSON.stringify(data) }, 'post')
      if (res) C.closeBrowserWindow(res)
    },

    tips: function (msg, type) {
      this.$message({
        message: msg,
        type: type
      })
    },

    times: function (unix) {
      return C.times(unix)
    },
    closewin () {
      C.closewin()
    }
  },
  computed: {
    accoumoney: function () {
      let acc = 0
      if (this.accountlist.length) {
        for (var i = 0; i < this.accountlist.length; i++) {
          acc += this.accountlist[i].paymoney * 1
        }
      }
      return parseFloat(this.money - acc).toFixed(2)
    },
    allotmoney: function () {
      let usr = 0
      if (this.userlist.length) {
        for (var i = 0; i < this.userlist.length; i++) {
          usr += this.userlist[i].feat * 1
        }
      }
      return parseFloat(this.money - usr).toFixed(2)
    }
  },
  mounted: async function () {
    let res = await C.ajax('/shop/backcard/getbackcardinfo', { id: this.request.id }, 'post')
    if (res) {
      this.money = res.back.isstore ? 0 : res.back.money
      this.back = res.back
      this.backinfo = res.backinfo
    }
  }
}
</script>
<style>
.ov {
    overflow: hidden;
}

.ov-x {
    overflow-x: hidden;
}

.fl {
    float: left;
}

.fr {
    float: right;
}

.clear {
    clear: both;
}

.none {
    display: none;
}

.block {
    display: block;
}

.handel {
    cursor: pointer;
}




/*背景颜色-前景颜色*/

.bg-dark {
    background: #F9FAFC;
}

.bg-extra {
    background: #EFF2F7;
}

.bg-light {
    background: #E5E9F2;
}

.bg-gray {
    background: #D3DCE6;
}

.bg-silver {
    background: #8492A6;
}

.bg-black {
    background: #324057;
}

.white {
    color: white;
}

.black {
    color: #324057;
}

.green {
    color: green;
}

.blue {
    color: blue;
}

.red {
    color: red;
}

.color-nan {
    color: #2a91f7;
}

.color-nv {
    color: #f7992a;
}




/*其他样式*/

html {
    font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, SimSun, sans-serif;
    font-weight: 400;
}

.title {
    font-size: 16px;
    font-family: "Helvetica Neue", "Helvetica", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
    line-height: 26px;
    margin: 10px 0;
}

.border {
    border: 1px solid #dfe6ec;
    border-radius: 4px;
    padding: 0 8px;
}

.bd {
    border: 1px solid #dfe6ec;
}

.border-b {
    border-bottom: 1px solid #dfe6ec;
}

.border-t {
    border-top: 1px solid #dfe6ec;
}

.bd-l {
    border-left: 1px dashed #dfe6ec;
}

.bd-t {
    border-top: 1px dashed #dfe6ec;
}

.bd-r {
    border-radius: 50%;
}

.underline {
    text-decoration: underline;
}




/*边距*/

.p0 {
    padding: 0 !important;
}

.m0 {
    margin: 0 !important;
}

.m10 {
    margin: 10px !important;
}

.m15 {
    margin: 15px !important;
}

.p10 {
    padding: 10px !important;
}

.p15 {
    padding: 15px !important;
}

.ml {
    margin-left: 15px !important;
}

.mr {
    margin-right: 15px !important;
}

.mb {
    margin-bottom: 15px !important;
}

.mt {
    margin-top: 15px !important;
}

.pl {
    padding-left: 15px !important;
}

.pl0 {
    padding-left: 0 !important;
}

.pr {
    padding-right: 15px !important;
}

.pr0 {
    padding-right: 0 !important;
}

.pb {
    padding-bottom: 15px !important;
}

.pt {
    padding-top: 15px !important;
}

.center {
    text-align: center !important;
}

.left {
    text-align: left !important;
}

.right {
    text-align: right !important;
}

.w100 {
    width: 100px !important;
}

.w150 {
    width: 150px !important;
}

.w200 {
    width: 200px !important;
}

.w300 {
    width: 300px !important;
}

.w400 {
    width: 400px !important;
}

.w500 {
    width: 500px !important;
}

.w30 {
    width: 30%;
}

.w50 {
    width: 50%;
}

.width95 {
    width: 95%;
}

.width {
    width: 100% !important;
}

.h200 {
    height: 200px;
}

.h300 {
    height: 300px;
}

.h500 {
    height: 500px;
}

.h600 {
    height: 600px;
}

.h700 {
    height: 700px;
}

.h800 {
    height: 800px;
}

.l28 {
    line-height: 28px;
}

.l30 {
    line-height: 30px;
}

.l50 {
    line-height: 50px;
}




/*附加订制样式*/

.el-row {
    margin: 0 !important;
}

.el-table .cell {
    padding: 0;
    text-align: center;
}

.el-textarea__inner {
    resize: none;
}

.el-input.is-disabled .el-input__inner {
    color: #20A0FF;
}

.el-textarea.is-disabled .el-textarea__inner {
    color: #444;
}

.el-switch {
    line-height: 0;
}

.el-checkbox+.el-checkbox {
    margin-left: 0;
}

.el-checkbox {
    margin-right: 20px;
}

.el-autocomplete-suggestion li {
    padding: 4px 20px;
    line-height: 20px;
}

.el-table th {
    padding: 10px 0;
}

.el-table td {
    padding: 4px 0;
}
</style>